﻿using AutoMapper;
using BLL.Interface;
using DAL;
using DAL.Interface;
using Model;
using Model.DTO;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BLL
{
    /// <summary>
    /// 基础BLL类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public  class BaseBLL<T> : IBaseBLL<T> where T : Common, new()
    {
        /// <summary>
        /// 依赖注入IBaseDAL
        /// </summary>
        private readonly IBaseDAL<T> BaseDAL;
        /// <summary>
        /// 依赖注入IMapper（自动映射实体类）
        /// </summary>
        public IMapper mapper { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseDAL"></param>
        public BaseBLL(IBaseDAL<T> baseDAL)
        {
            BaseDAL = baseDAL;
        }
        /// <summary>
        /// 数据上下文
        /// </summary>
        /// <returns></returns>
        public MyDbContext DbContext()
        {
            return BaseDAL.DbContext();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> Add(T entity)
        {
            return await BaseDAL.Add(entity);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> Delete(int id)
        {
            T re = await BaseDAL.Find(id);
            if (re != null)
            {
                return await BaseDAL.Delete(re);
            }
            return false;

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> Update(T entity)
        {
            return await BaseDAL.Update(entity);
        }
        /// <summary>
        /// 查询一个，通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> Find(int id)
        {
            return await BaseDAL.Find(id);
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public async Task<DataList<T>> List()
        {
            List < T > objs= await BaseDAL.List();
           return new DataList<T>() { Total=objs.Count,List=objs};
        }
        /// <summary>
        /// 查询少量，默认5条
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<List<T>> List(int limit = 5)
        {
            return await BaseDAL.List(limit);
        }
        /// <summary>
        /// 查询列表，通过id列表
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
       public async Task<List<T>> List(List<int> ids)
        {
            return await BaseDAL.List(ids);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<DataList<T>> Pagination(Limit limit)
        {
            List<T> list = await BaseDAL.Pagination(limit);
            return new DataList<T>() { Total = list.Count, List = list };
        }


    }
}